package com.gitee.huxing.course.netty.client;

import com.gitee.huxing.course.netty.gateway.factory.BusinessFactory;
import com.gitee.huxing.course.netty.util.SpringBeanContext;
import io.netty.channel.*;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import sun.nio.ch.Net;

import java.nio.charset.StandardCharsets;

/**
 * @Description: 客户端响应处理
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.netty.client
 * @ClassName: ClientHandler
 * @Author: huxing
 * @DateTime: 2021-08-19 下午1:55
 */
@Slf4j
@SuppressWarnings("all")
public class HttpClientHandler extends ChannelInboundHandlerAdapter{

    /** 消息处理 **/
    private ChannelHandlerContext context;

    /** netty 客户端对象 **/
    private NettyClient nettyClient;

    public HttpClientHandler(){
    }

    public HttpClientHandler(ChannelHandlerContext context){
        this.context = context;
    }

    public HttpClientHandler(NettyClient nettyClient){
        this.nettyClient = nettyClient;
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("connected server. start send msg.");
        super.channelActive(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
        System.out.println("ctx has removed.");
    }

    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg){
        try {
            FullHttpResponse response = (FullHttpResponse) msg;
            // 获取处理对象
            this.context = nettyClient.getContext();
            // 消息处理
            if (context != null) {
                // 响应处理
                log.info("网关响应消息过滤: {}", response.content().toString(StandardCharsets.UTF_8));
                BusinessFactory factory = (BusinessFactory) SpringBeanContext
                        .getBean("responseFactory");
                factory.processBussiness(response, context);
            } else {
                // 打印处理的消息
                String requestTime = response.headers().get("requestTime");
                String responseTime = response.headers().get("responseTime");
                String body = response.content().toString(StandardCharsets.UTF_8);
                log.info("请求时间: {}", requestTime);
                log.info("响应时间: {}", responseTime);
                log.info("客户端收到响应消息: {}", body);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("exceptionCaught");
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelUnregistered");
        super.channelUnregistered(ctx);
    }


}
